<template>
  <div class="set-container">
    <h1>网站信息</h1>
    <el-form :model="form" disabled label-position="top">
      <el-form-item label="网站标题">
        <el-input v-model="form.siteTitle" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.mail" />
      </el-form-item>
      <el-form-item label="备案号">
        <el-input v-model="form.icp" />
      </el-form-item>
      <el-divider />
      <el-form-item label="网站头像信息">
        <el-image style="width: 100px; height: 100px" :src="form.avatar" />
      </el-form-item>
      <el-divider />

      <h1>网址图标信息</h1>
      <el-form-item label="网址图标地址">
        <el-input v-model="form.favicon" />
      </el-form-item>
      <el-form-item label="网址图标预览">
        <el-image style="width: 100px; height: 100px" :src="form.favicon" />
      </el-form-item>
      <el-divider />

      <h1>github 信息</h1>
      <el-form-item label="github 名字">
        <el-input v-model="form.githubName" />
      </el-form-item>
      <el-form-item label="github 地址">
        <el-input v-model="form.github" />
      </el-form-item>
      <el-divider />

      <h1>QQ 信息</h1>
      <el-form-item label="QQ 号码">
        <el-input v-model="form.qq" />
      </el-form-item>
      <el-form-item label="QQ 二维码图片预览">
        <el-image style="width: 100px; height: 100px" :src="form.qqQrCode" />
      </el-form-item>
      <el-divider />

      <h1>微信信息</h1>
      <el-form-item label="微信号">
        <el-input v-model="form.weixin" />
      </el-form-item>
      <el-form-item label="QQ 二维码图片预览">
        <el-image
          style="width: 100px; height: 100px"
          :src="form.weixinQrCode"
        />
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <el-button type="primary" @click="openEditPanel">进入编辑模式</el-button>

    <el-dialog
      title="请编辑信息"
      :visible.sync="dialogFormVisible"
      width="50%"
      top="5vh"
      fullscreen
    >
      <el-form :model="form" label-position="top">
        <el-form-item label="网站标题">
          <el-input v-model="form.siteTitle"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.mail"></el-input>
        </el-form-item>
        <el-form-item label="备案号">
          <el-input v-model="form.icp"></el-input>
        </el-form-item>
        <el-form-item label="网站头像地址">
          <!-- <el-input v-model="form.avatar"></el-input> -->
          <Upload v-model="form.avatar" />
        </el-form-item>
        <el-form-item label="网站图标地址">
          <el-input v-model="form.favicon"></el-input>
        </el-form-item>
        <el-form-item label="github 名字">
          <el-input v-model="form.githubName"></el-input>
        </el-form-item>
        <el-form-item label="github 地址">
          <el-input v-model="form.github"></el-input>
        </el-form-item>
        <el-form-item label="QQ 号码">
          <el-input v-model="form.qq"></el-input>
        </el-form-item>
        <el-form-item label="QQ 二维码图片地址">
          <!-- <el-input v-model="form.qqQrCode"></el-input> -->
          <Upload v-model="form.qqQrCode" />
        </el-form-item>
        <el-form-item label="微信号">
          <el-input v-model="form.weixin"></el-input>
        </el-form-item>
        <el-form-item label="微信二维码图片地址">
          <!-- <el-input v-model="form.weixinQrCode"></el-input> -->
          <Upload v-model="form.weixinQrCode" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmEditSetting">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSetting, setSetting } from "@/api/setting";
import Upload from "@/components/Upload";
import { server_URL } from "@/urlConfig";

export default {
  name: "Set",
  components: { Upload },
  data() {
    return {
      form: {
        avatar: "",
        favicon: "",
        github: "",
        githubName: "",
        icp: "",
        id: "",
        mail: "",
        qq: "",
        qqQrCode: "",
        siteTitle: "",
        weixin: "",
        weixinQrCode: "",
      },
      dialogFormVisible: false,
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      getSetting().then((res) => {
        this.form = res.data;
        this.form.avatar = server_URL + this.form.avatar;
        this.form.qqQrCode = server_URL + this.form.qqQrCode;
        this.form.weixinQrCode = server_URL + this.form.weixinQrCode;
      });
    },
    openEditPanel() {
      this.dialogFormVisible = true;
    },
    confirmEditSetting() {
      setSetting(this.form).then(() => {
        this.dialogFormVisible = false;
        this.fetchData();
        this.$message.success("修改成功！");
      });
    },
  },
};
</script>

<style scoped lang="scss">
.set-container {
  padding: 20px;
  h1 {
    font-size: 24px;
    color: #606266;
    line-height: 40px;
  }
}
</style>
